package com.zhuiyun.pm.client.login.Code;/**
 * @author syc
 * @date 2023/3/28 19:06
 */

import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import lombok.Data;
import org.springframework.stereotype.Component;


import java.awt.*;

/**
 * @ClassName LoginProperties
 * @Description TODO
 * @Author sgo
 * @Date 2023/3/28 19:06
 **/
@Component
public class CodeUtils {
    
    /**
     * 依据配置信息生产验证码
     * @param
     * @return
     */
    public Captcha switchCaptcha(){
        Captcha captcha = null;
//        设置验证码大小
//        captcha = new FixedArithmeticCaptcha(111,36);  算输验证码
//        生成英文混合验证码
        captcha = new SpecCaptcha(111,36);
//        验证码长度
        captcha.setLen(4);
//    设置验证码字体
         captcha.setFont(new Font("Fixedsys",Font.PLAIN,25));
        return captcha;
    }

    static  class FixedArithmeticCaptcha extends ArithmeticCaptcha {
        public FixedArithmeticCaptcha(int width,int height){
            super(width,height);
        }

        @Override
        protected char[] alphas() {
            // 生成随机数字和运算符
            int n1 = num(1, 10), n2 = num(1, 10);
            int opt = num(3);

            // 计算结果
            int res = new int[]{n1 + n2, n1 - n2, n1 * n2}[opt];
            // 转换为字符运算符
            char optChar = "+-x".charAt(opt);

            this.setArithmeticString(String.format("%s%c%s=?", n1, optChar, n2));
            this.chars = String.valueOf(res);

            return chars.toCharArray();
        }
    }
}
